package lab;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

public class CmdUtil {
	
	public static List<String> execute(String cmd) throws IOException {
		return execute(cmd,null);
	}
	
	public static List<String> execute(String cmd,String regx) throws IOException{
		Process p = Runtime.getRuntime().exec(cmd);
		BufferedReader reader = null;
		List<String> msgs = null;
		try{
			reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
			msgs = new ArrayList<String>();
			String line = null;
			Pattern ptn = (regx==null?null:Pattern.compile(regx));
			while((line = reader.readLine())!=null){
				if(ptn==null || ptn.matcher(line).matches()){
					msgs.add(line);
				}
			}
		}finally{
			try {
				reader.close();
			} catch (IOException e) {
			}
		}
		return msgs;
	}
}
